﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.ComponentModel;

public partial class extends_RecordControl : System.Web.UI.UserControl
{
    private String _query="";      //查询语句
    private int _page=0;      // 当前页
    private String _where="";     // 条件
    private int _pagesize=20;     // 页大小
    private int recordnum = 0;
    private int pagenum = 0;
    private int showpage = 0;
    private string _operate="修改,删除";
    private string _mainKey="ID";
    private string order = "";
    private string _imageFields="";
    private string _hideKey="";
    public StringBuilder outContext = new StringBuilder();


    public string hideKey
    {
        get { return _hideKey; }
        set { _hideKey = value; }
    }

    public string ImageFields
    {
        get { return _imageFields; }
        set { _imageFields = value; }
    }

    public string Order
    {
        get { return order; }
        set { order = value; }
    
    }
    public string mainKey
    {
        get { return _mainKey; }
        set { _mainKey = value; }
    }
    public int pageSize
    {
        get { return _pagesize; }
        set { _pagesize = value; }
    }

    public String where
    {
        get { return _where; }
        set { _where = value; }
    }
    public String  Query
    {
        get { return _query; }
        set 
        {
            _query = value;
            _query=_query.ToUpper();
        }
    }

    
    public string OperateSet
    {
        get { return _operate; }
        set 
        {
            _operate = value;
        }
    }

    [Browsable(true)]
    [Category("Appearance")]
    [Description("上次的查询语句")]
    [DefaultValue("0")]
    public int currentPage
    {
        set
        {
            ViewState["currentPage"] = value;
        }
        get
        {
            if (ViewState["currentPage"] == null)
            {
                return 0;
            }
            else
                return Convert.ToInt32(ViewState["currentPage"]);
        }
    }


    [Browsable(true)]
    [Category("Appearance")]
    [Description("上次的查询语句")]
    [DefaultValue("")]
    private string HistorySQL
    {
        set
        {
            ViewState["HistorySQL"] = value; 
        }
        get 
        {
            if (ViewState["HistorySQL"] == null || ViewState["HistorySQL"].ToString() == "")
            {
                return Query;
            }
            else
                return Convert.ToString(ViewState["HistorySQL"]);
        }
    }

    [Browsable(true)]
    [Category("Appearance")]
    [Description("上次的查询结果")]
    [DefaultValue("")]
    private DataTable TableValue
    {
        set
        {
            ViewState["TableValue"] = value;
        }
        get
        {
            if (ViewState["TableValue"] == null)
                return null;
            else
                return (DataTable)ViewState["TableValue"];
        }
    
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindData(_query);
        }
        string Javascript = "<script type='text/javascript'>";
        Javascript = Javascript + "function localPage" + this.UniqueID + "(page){";
        Javascript = Javascript + " __doPostBack('" + PageButton.UniqueID + "',page);";
        Javascript = Javascript + "}</script>";
        ScriptManager.RegisterStartupScript(this, this.GetType(), "", Javascript, false);
    }
    public void UpdataPage()
    {
        _page = currentPage;
        BindData(HistorySQL);
    }
    private bool isHideFiled(string []fileds,string name)
    {
        if (fileds == null)
            return false;
        for (int i = 0; i < fileds.Length; i++)
        {
            if (fileds[i] == name)
                return true;
        }
        return false;
    }
    private void BindData(String SQL)
    {
        if (_query == null || _query == "")
        {
            return;
        }
         SQL = SQL.Trim();
         string[] items = OperateSet.Split(',');
         if (items.Length > 0)
         {
             SQL = SQL.Substring(0, 6) +" "+ mainKey.ToString() + ", " + SQL.Substring(6);
         }
         outContext.Remove(0, outContext.Length);

         SQL = SQL + " " + order;

        EntityOprHelper helper = new EntityOprHelper();
        
        DataTable _table = helper.GetDataTableBySql(SQL);

        string[] hideFields=_hideKey.Split(',');

        if (_table != null)
        {
            outContext.Append("<table class=table cellspacing=1 cellpadding=3 width='98%' align=center border=0>");

            DataColumnCollection columns = _table.Columns;
            //输出头信息
            int count = columns.Count;
            for (int i = 1; i < count; i++)
            {

                if (!isHideFiled(hideFields, columns[i].ColumnName))
                {
                    outContext.Append("<td  align='center' class='xingmu'>");
                    outContext.Append(columns[i].ColumnName);
                    outContext.Append("</td>");
                }
            }
            
            if (items.Length > 0)
            {
                outContext.Append("<td  align='center' class='xingmu'>");
                outContext.Append("操作");
                outContext.Append("</td>");
            }

            DataRowCollection rows = _table.Rows;
            recordnum = rows.Count;
            pagenum = recordnum / _pagesize;
           

            if (recordnum % _pagesize > 0)
                pagenum = pagenum + 1;

            showpage = _page+1;

            if (recordnum < 1)
                showpage = 0;

            int rownum = rows.Count;

            string []imageLists=_imageFields.Split(',');

            for (int i = _page * _pagesize; i < rownum && i < (_page +1)* +_pagesize; i++)
            {

                DataRow row = rows[i];
                outContext.Append("<tr>");
                for (int j = 1; j < count; j++)
                {
                    if (!isHideFiled(hideFields, columns[j].ColumnName))
                    {

                        outContext.Append("<td height='25' align='center' class='hback'>");
                        if (imageLists != null)
                        {
                            string appString = row[j].ToString();
                            for (int t = 0; t < imageLists.Length; t++)
                            {
                                if (imageLists[t].ToString() == columns[j].ColumnName)
                                {
                                    if (appString != null && appString != "")
                                        appString = "<img src='" + appString + "' width='60' height='40'>";
                                    break;
                                }
                            }
                            outContext.Append(appString);
                        }
                        else
                            outContext.Append(row[j].ToString());
                        outContext.Append("</td>");
                    }
                }



                if (items.Length > 0)
                {
                    outContext.Append("<td height='25' align='center' class='hback'>");
                    String other="";
                    for (int k = 0; k < items.Length; k++)
                    {
                        if (k > 0)
                            other = other + "｜";

                        string showValue = items[k];
                        string[] _info = showValue.Split('|');
                        if (_info.Length !=3)
                            other = other + " <a href=\"javaScript:SelectItemed('" + this.ID + "','" + k.ToString() + "','" + row[0].ToString() + "')\">" + showValue + "</a>";
                        else
                        {
                            showValue = _info[0];
                            string fieldValue=row[_info[1]].ToString();
                            if (fieldValue != null && (fieldValue==_info[2]))
                            {
                                other = other + " <a href=\"javaScript:UnSelectItemed('" + this.ID + "','" + k.ToString() + "','" + row[0].ToString() + "')\">" + "<font color='0xFFBB00'> " + showValue + "</font></a>";
                            }
                            else
                            {
                                other = other + " <a href=\"javaScript:SelectItemed('" + this.ID + "','" + k.ToString() + "','" + row[0].ToString() + "')\">" + showValue + "</a>";
                            }

                        }
                    }
                    outContext.Append(other);
                    outContext.Append("</td>");
                }

                outContext.Append("<tr>");
            }


            outContext.Append(" <TR align=right><TD class=hback colSpan=13 height=32>");

            if (_page > 0)
            {

                outContext.Append(" <A title='首页'  href='JavaScript:localPage" + this.UniqueID + "(0)'>");
                outContext.Append(" <FONT title='首页' face='webdings'>9</FONT>");
                outContext.Append(" </A>");
            }
            else
            {
                outContext.Append(" <FONT title='首页' color='#999999'>");
                outContext.Append(" <FONT title='首页' face='webdings'>9</FONT>");
                outContext.Append(" </FONT>");
            }

            if (_page >= 10)
            {
                outContext.Append(" <A title='上十页'  href='JavaScript:localPage" + this.UniqueID + "(" + (_page - 10).ToString() + ")'>");
                //outContext.Append(" <FONT title='上十页'>");
                outContext.Append(" <FONT title='上十页' face='webdings'>7</FONT>");
                //outContext.Append(" </FONT>");
                outContext.Append(" </A>");
            }
            else
            {
                outContext.Append(" <FONT title='上十页' color='#999999'>");
                outContext.Append(" <FONT title='上十页' face='webdings'>7</FONT>");
                outContext.Append(" </FONT>");
            }

            if (_page > 0)
            {
                outContext.Append(" <A title='上十页'  href='JavaScript:localPage" + this.UniqueID + "(" + (_page - 1).ToString() + ")'>");
                outContext.Append(" <FONT title='上一页' face='webdings'>3</FONT>");
                outContext.Append(" </A>");
            }
            else
            {
                outContext.Append(" <FONT title='上一页' color='#999999'>");
                outContext.Append(" <FONT title='上一页' face='webdings'>3</FONT></FONT>");
            }

            int snum = showpage;
            if (snum > 3)
            {
                snum = snum - 3;
            }
            else
            {
                snum = 0;
            }
            for (int i = snum; i < snum+8 && i < pagenum; i++)
            {
                if(i==_page )
                   outContext.Append("  <B>"+(i+1).ToString()+"</B> ");
                else
                   outContext.Append("  <A  href='JavaScript:localPage" + this.UniqueID + "(" + (i).ToString() + ")'>" + (i + 1).ToString() + "</A>  ");
            }



            if (showpage < pagenum)
                outContext.Append("  <A title='下一页'  href='JavaScript:localPage" + this.UniqueID + "(" + (showpage).ToString() + ")'> <FONT title='下一页'  face='webdings'>4</FONT></A> ");
            else
            {
                //outContext.Append(" <FONT title='上一页' color='#999999'>");
                outContext.Append("  <FONT title='下一页'   color='#999999'> <FONT title='下一页'  face='webdings'>4</FONT></FONT> ");
            }

            if (showpage < pagenum-10)
            {
                outContext.Append(" <A title='下十页'  href='JavaScript:localPage" + this.UniqueID + "(" + (showpage + 9).ToString() + ")'>");
                outContext.Append("  <FONT title=下十页  face='webdings'>8</FONT> </A>");
            }
            else
            {
                outContext.Append(" <FONT title='下十页'  color='#999999'>");
                outContext.Append("  <FONT title=下十页  face='webdings'>8</FONT> </FONT>");
            }
            if (showpage < pagenum)
            {
                outContext.Append("  <A title='尾页' href='JavaScript:localPage" + this.UniqueID + "(" + (pagenum - 1).ToString() + ")'> ");
                outContext.Append("  <FONT title='最后一页' face='webdings'>:</FONT> </A>");
            }
            else
            {
                outContext.Append("  <FONT title='尾页' color='#999999'> ");
                outContext.Append("  <FONT title='最后一页' face='webdings'>:</FONT> </FONT>");
            }

            outContext.Append("  &nbsp;每页<B>" + _pagesize.ToString() + "</B>个记录，现在是:<B><SPAN class=tx>" + showpage.ToString() + "</SPAN>/" + pagenum.ToString() + "</B>页，共<B>");

            outContext.Append("  <SPAN id=recordcount>"+recordnum.ToString()+"</SPAN></B>个记录。</TD></TR> ");



            outContext.Append("</table>");


             printblock.InnerHtml = outContext.ToString();
        }
    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        if (keyword.Text == "")
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "请输入关键字", true);
            return;
        }
        String SQL = _query + " where " + _where + " like '%" + keyword .Text + "%'";
        ViewState["HistorySQL"] = SQL;
        BindData(SQL);
    }


    protected void PageButton_Click(object sender, EventArgs e)
    {
        String nPage = Request["__EVENTARGUMENT"].ToString();
        _page = Convert.ToInt32(nPage);
        currentPage = _page;
        String SQL = HistorySQL;
        BindData(SQL);
    }

}
